SWINGS

AWT Component Replacements

The Swing component set was originally created because the basic AWT Components that came with the original version of the java libraries ere insufficient for real-world, forms-based  applications . All the basic components were there, but the existing set was too small and far too restrictive . For instance,  you  could n’t even put an image on a button . To alleviate this situation , the Swing component set offers replacements for each of the AWT  components . The Swing components support all the capabilities of the original set and offer a whole lot more besides .As such, you should never need to deal with any of the basic AWT components.

            The basic distinction between the Swing and equivalent AWT components is, in  most  cases ,  the Swing component class names begin with a J  and  the AWT  ones don’t  . Swing’s  JButton  is a replacements for the AWT  Button component. One exception is the JComboBox, which replaces the AWT Choice component .

            At the application programming interface (API) level , the Swing components are almost  always a super set of the features the AWT components support . While they support additional capabilities,the basic AWT capabilities are there  for everything but the JList component, whose API is completely unlike that of the AWT List component.

 

AWT  Component   Nearest Swing Replacement

Button

JButton

Canvas

JPanel

Checkbox

JCheckBox

Checkbox CheckboxGroup

JRadioButton in
ButtonGroup

Choice

JComboBox

Component

JComponent

Container

JPanel

Label

JLabel

List

JList

 

SWING OVERVIEW

   Note For most people ,  the fact that the Swing  components  replace  AWT Components is  irrelevant.Just treat the Swing  components as an  independent Component set, and you’ll be perfectly okay.To help you understand how to use the Swing components. With just a  single line of text as  its label, like an AWT Button ,but adds capabilities , such as using image icons on buttons and working with multiple lines of text.

            In addition to replacing each of the basic components, the Swing component Set has a replacement for the higher-level window objects.  Although the only change in most of the components’names is the beginning J, Swing’s replacement for the old FileDialog object.   

   


Menu

JMenu

MenuBar

JMenuBar

MenuItem

JMenuItem

Panel

JPanel                                                                                                                                                   

PopupMenu

JPopupMenu                                                                                                                                                   

Scrollbar

JScrollBar

ScrollPane

JScrollPane

TextArea

JTextArea

TextField

JTextField                                                        

Applet

JApplet

Dialog

JDialog

FileDialog

JFileChooser

Frame

JFrame

Window

JWindow

                                                                                                                        
Non-AWT Upgraded Components

     In addition to offering replacements for all the basic AWT components, the Swing Component set includes twice as many new components.

    Note   If you’re  new to Java,just think of all of these components both the AWT component replacements and those that were not in the AWT-as one big set of components, versus two distinct sets. Here’s a look at those components that didn’t originate in the AWT world.

JPasswordField: This specialized text field is for password entry, as shown in you cannot use cut or copy operations within the component, but you can paste text into  it.

  JEditorPane and JTextPane: These two components provide support for displaying and editing multiple-attributed content, such as an HTML and RTF viewer.

JSpinner: this component ,shown in Figure 1-4,provides selection from an ordered Set of predefined values, offering arrows to scroll through the next and previous choices. The predefined values can be  an array   of strings, a sequential set  of numbers, or a date.

JToggleButton: this component offers a button that stays depressed when selected.

JSlider: This component is like scrollbar component of AWT(or JScrollBar in the Swing component set). However, its purpose in  Swing  is for user input. It offers various clues to help the user choose a value.

JProgressBar: This component allows the user to visually   see the progress of an activity.

JFormateTextField: This component provides for the input of formatted text, like numeric values, phone numbers, dates, or social security numbers.

JTable: This component provides for the display of two-dimensional row and column information.

JTree: This component supports the display of hierarchical data.

JToolTip: Through this component, all Swing components support  pop-up text for Offering useful  tips. Figure1-11 shows an example of  a  JToolTip component added  to a JSlider.

JToolBar: This container offers a  draggable  toolbar to be included within any program window.

JRadioButtonMenuItem: This component is an addition to the set of menu components. With it, you can have radio buttons on a menu for mutually exclusive choices.

JSeparator: The menu’s separator bar is now its own component and can be used outside of  menus.

JDesktopPane and JInternalFrame:  This pair of components allows you to develop applications using the familiar Windows  MultipleDocumentInterface(MDI).

JOptionPane: This component allows you to easily create pop-up windows with varied  content .

JColorChooser: This component is for choosing a  color , with different  views available to selected the color.

JSplitPane: This container allows you to place multiple components in a window. It also allows the user control over how much of each component is visible.

JTabbedPane: This component is like a container whose layout manager is CardLayout (discussed in chapter 10), with labeled tabs automatically provided to allow the user to swap cards. This provides you with the familiar property-sheet motif.

 

JApplet: JApplet class which extends applet .The difference between Applet and  JApplet is when adding a component to an instance of JApplet , don’t invoke the add method of the Applet  .

METHOD:
void add(component)

Container can be use to add a component.
                                   

Icon&Labels: Icons are encapusalated  by the image icon class.

Constructor:             
ImageIcon(String,filename)
ImageIcon(URL)

Methods
getIconHeight( )
getIconWidth( )

 

JLabel :  Labels are instance of the JLabel class which extends JComponent.

                                    Constructor:
JLabel (Icon)
JLabel(String)
JLabel(String,Icon,Align)        where Align = Right or Center

Methods:
IcongetIcon( )
String getText( )
void setIcon(Icon)
void setString(String)

JTextField: TextField  is encapsulated by the JText  component class which extends JComponent.

           Constructor:
JTextField( )
JTextField( columns)
JTextField(String, columns)
JTextField(String)

Methods: String getText( );
void setText ( String);

Buttons: Swing buttons are subclasses of the abstract button class which extends JComponents . Abstract button contains means method they control the behaviour of buttons , checkboxes and radio buttons.

             Method:
                          void setdisabledIcon(Icon)
void setPressedIcon(Icon)
void set Selected Icon(Icon)
void setRolloverIcon(Icon)

Me Listener are

void  addActionListener(ActionListener )
void  removeActionListener(actionListener)

JButton:  JButton  allows an icon a String on both.

     Construtor:
JButton (Icon)
JButton (String)
JButton (String,Icon)

 

JCheckBoxes:  Which provides the functionality of a checkbox

Construtor:
JCheckBox  (Icon)
JCheckBox ( Icon, Boolean)  
JCheckBox (String)
JCheckBox (String, boolean)
JCheckBox (String, Icon)                  
JCheckBox (String, Icon, Boolean)

Method:

                                    void setSelected (Boolean)
void  itemStateChanged ( )

JRadioButtons:  RadioButtons are supported by the radioButton class.

Construtor:
JRadioButton (Icon)
JRadioButton (Icon, Boolean)
JRadioButton (String)
JRadioButton (String, Boolean)
JRadioButton (String, Icon)
JRadioButton (String, Icon,Boolean)       

Radio buttons  must be consigned into a group. Me button group class is instanciated  to create a button group elements are then added to the button group to the following method.

                 void add (adstract button )
ActionEvent methods  are
actionPerformed (  )
getActionCommand( )

 

JComboBox:  Cmbobox  provides  a textField  of dropdown list.

Constructer:
JComboBox( );
JComboBox(vector)

          Method:
void addItem(object)

JTabbedPane: Tabbed pane is a component that appears as a group of folders in a file cabinet – tabbed pans are encapsulated by the JTabbedPane class .

           Constructer :
JTabbedPane( );

Method: void addTabbedPane(String,Component)

JScrollPane: Horizontal or vertical scrollbars may be provided if necessary.
        
Constructor:
JScrollPane(Component)
JScrollPane(bsb,hsd)
JScrollPane(Component,vsb,hsb)
       Consants:
 
HORIZONTAL_SCROLLBAR_ALWAYS
HORIZONTAL_SCROLLBAR_AS_NEEDED

          VERTICAL_SCROLLBAR_ALWAYS
VERTICAL_SCROLLBAR_AS_NEEDED

   JTree: ATree is a component that presents a hierarchical view of data

      Constructer :
JTree(hashtable)
JTree(object)
JTree(treenode)
JTree(vector)

A Tree object generates events when a node is expanded or collapsed
Method:
addTreeExpansionListener (TreeExpansionListener)
void  removeTreeExpansionListener (TreeExpansionListener)
void treeCollapsedExpansionListener (TreeExpansionEvent)
void treeExpanded (TreeExpansionEvent)
void addMultableTreeNode

     JTable: A Table is a component that displays rows and columns of data.

Constructer :
JTable(object [][])    object columns

 

The Swing component classes that are used this book are
Class                                                                                              Description
Abstract Button                                         Abstract Super class follows Swing Buttons
Button Group                                                         Encapsulates a mutually exclusive set of Button ImageIcon      Encapsulates an Icon
JApplet                                                                               the Swing version of Applet
JButton                                                                              The Swing push button class
JCheckBox                                                             The Swing checkbox class
JComboBox                                                           Excapsulates a comboBox(a combination of
adrop_down  List and TextField)           
JLabel                                                                                 The Swing version of a label
JRadioButton                                                         The Swing version of a RadioButton
JScrollPane                                                             Encapsulates a Scroll window
JT   abbedPane                                                       Encapsulates a Tabbed window
JTable                                                                                 Encapsulates a Table_based control
JTextField                                                             The Swing version of  a TextField
JTree                                                                                               Encapsulates a Tree based control

The Swing related classes are contained in Javax_Swing and its  subpackages
such as Javax.Swing.Tree